#define _CRT_SECURE_NO_WARNINGS 1

  struct ListNode {
 	int val;
 	struct ListNode *next;
 	ListNode(int x) : val(x), next(nullptr) {}
  };
 
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head)
    {
        ListNode* cur = head;
        ListNode* prev = nullptr;
        while (cur)
        {
            if (cur->next && cur->val == cur->next->val)
            {
                int del_val = cur->val;
                while (cur && cur->val == del_val)
                {
                    ListNode* del_node = cur;
                    cur = cur->next;
                    delete del_node;
                }
                if (prev == nullptr)
                {
                    head = cur;
                }
                else
                {
                    prev->next = cur;
                }
            }
            else
            {
                prev = cur;
                cur = cur->next;
            }
        }
        return head;
    }
};